﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace MM.Common.DBBase
{
    public class Logger
    {
        // Fields
        public bool EnableExp = true;
        public bool EnableLog = true;
        private string ExpPath;
        private int FolderLoop;
        private string Location;
        private string LogPath;
        private int MaxLoop = 0x800;
        private int WriteExpLoop;
        private int WriteLogLoop;

        // Methods
        public Logger(string loc)
        {
            this.Location = loc;
            this.CreateLogFile();
            this.CreateExpFile();
        }

        private void CreateExpFile()
        {
            this.ExpPath = AppDomain.CurrentDomain.BaseDirectory;
            try
            {
                this.ExpPath = this.ExpPath + "App_Data";
                if (!Directory.Exists(this.ExpPath))
                {
                    Directory.CreateDirectory(this.ExpPath);
                }
                this.ExpPath = this.ExpPath + @"\log";
                if (!Directory.Exists(this.ExpPath))
                {
                    Directory.CreateDirectory(this.ExpPath);
                }
                this.ExpPath = this.ExpPath + @"\" + getFolderName();
                if (!Directory.Exists(this.ExpPath))
                {
                    Directory.CreateDirectory(this.ExpPath);
                }
                this.ExpPath = this.ExpPath + @"\" + getExpFileName();
                if (!File.Exists(this.ExpPath))
                {
                    File.CreateText(this.ExpPath).Close();
                }
            }
            catch (Exception exception)
            {
                if (this.FolderLoop <= this.MaxLoop)
                {
                    this.FolderLoop++;
                    this.CreateExpFile();
                }
                else
                {
                    this.FolderLoop = 0;
                    this.Exp(exception);
                }
            }
            finally
            {
                this.FolderLoop = 0;
            }
        }

        private void CreateLogFile()
        {
            this.LogPath = AppDomain.CurrentDomain.BaseDirectory;
            try
            {
                this.LogPath = this.LogPath + "App_Data";
                if (!Directory.Exists(this.LogPath))
                {
                    Directory.CreateDirectory(this.LogPath);
                }
                this.LogPath = this.LogPath + @"\log";
                if (!Directory.Exists(this.LogPath))
                {
                    Directory.CreateDirectory(this.LogPath);
                }
                this.LogPath = this.LogPath + @"\" + getFolderName();
                if (!Directory.Exists(this.LogPath))
                {
                    Directory.CreateDirectory(this.LogPath);
                }
                this.LogPath = this.LogPath + @"\" + getLogFileName();
                if (!File.Exists(this.LogPath))
                {
                    File.CreateText(this.LogPath).Close();
                }
            }
            catch (Exception exception)
            {
                if (this.FolderLoop <= this.MaxLoop)
                {
                    this.FolderLoop++;
                    this.CreateLogFile();
                }
                else
                {
                    this.FolderLoop = 0;
                    this.Exp(exception);
                }
            }
            finally
            {
                this.FolderLoop = 0;
            }
        }

        public void Exp(Exception ex)
        {
            string str = string.Concat(new object[] { "ExceptionType:", ex.GetType(), ";Message:", ex.Message, ";Source:", ex.Source, ";StackTrace", ex.StackTrace, ";" });
            str = ex.ToString();
            this.writexp(str);
        }

        public void Exp(string str)
        {
            this.writexp(str);
        }

        private static string getExpFileName()
        {
            DateTime now = DateTime.Now;
            return string.Concat(new object[] { now.Year, "-", now.Month, "-", now.Day, ".exp" });
        }

        private static string getFolderName()
        {
            DateTime now = DateTime.Now;
            return (now.Year + "-" + now.Month);
        }

        private static string getLogFileName()
        {
            DateTime now = DateTime.Now;
            return string.Concat(new object[] { now.Year, "-", now.Month, "-", now.Day, ".log" });
        }

        private static string getTime()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
        }

        public void Info(bool b)
        {
            this.writelog(b.ToString());
        }

        public void Info(Exception ex)
        {
            if (ex != null)
            {
                string str = string.Concat(new object[] { "ExceptionType:", ex.GetType(), ";Message:", ex.Message, ";Source:", ex.Source, ";StackTrace", ex.StackTrace, ";" });
                str = ex.ToString();
                this.writelog(str);
            }
        }

        public void Info(int i)
        {
            this.writelog(i.ToString());
        }

        public void Info(float f)
        {
            this.writelog(f.ToString());
        }

        public void Info(string str)
        {
            this.writelog(str);
        }

        private void writelog(object obj)
        {
            if (this.EnableLog)
            {
                string str = obj.ToString();
                try
                {
                    if (File.Exists(this.LogPath))
                    {
                        TextWriter writer = File.AppendText(this.LogPath);
                        writer.WriteLine("[" + getTime() + " " + this.Location + "] " + str);
                        writer.Flush();
                        writer.Close();
                        writer = null;
                    }
                }
                catch (Exception exception)
                {
                    if (this.WriteLogLoop <= this.MaxLoop)
                    {
                        this.Info(str);
                        this.WriteLogLoop++;
                    }
                    else
                    {
                        this.WriteLogLoop = 0;
                        this.Exp(exception);
                    }
                }
            }
        }

        private void writexp(string str)
        {
            if (this.EnableExp)
            {
                try
                {
                    if (File.Exists(this.ExpPath))
                    {
                        TextWriter writer = File.AppendText(this.ExpPath);
                        writer.WriteLine("[" + getTime() + " " + this.Location + "] " + str);
                        writer.Flush();
                        writer.Close();
                        writer = null;
                    }
                }
                catch (Exception exception)
                {
                    if (this.WriteExpLoop <= this.MaxLoop)
                    {
                        this.Info(str);
                        this.WriteExpLoop++;
                    }
                    else
                    {
                        this.WriteExpLoop = 0;
                        this.Exp(exception);
                    }
                }
            }
        }
    }
}
